<%@page contentType="text/html" pageEncoding="UTF-8" import="java.io.*, java.sql.*, javax.sql.*, java.util.*" %>

<%@page import="com.att.network.acronym.Util"%>


<%
    request.setCharacterEncoding("UTF-8");
    String sql = request.getParameter("sql");
    if (sql != null) {
        sql = sql.trim();
    }


%>

<form method="post"  accept-charset="UTF-8">
    <textarea rows=10 cols=120 name="sql">
        <%= sql%>
    </textarea>
    <input type="submit" />
</form>

<%@include file="../init.jsp" %>
<%
    out.println(db);
    //create table acronym (id identity, name varchar(30), insert_time timestamp, text varchar(20), author integer);
    Connection con = null;

    if (sql != null) {

        try {

            con = Util.getCon(db);;
            con.getMetaData();
            if (sql.indexOf("select") >= 0) {
                ResultSet rs = con.createStatement().executeQuery(sql);
                ResultSetMetaData metaData = rs.getMetaData();

                int rowCount = metaData.getColumnCount();

                for (int i = 0; i < rowCount; i++) {

                    out.print(metaData.getColumnName(i + 1) + " \t");
                    out.print(metaData.getColumnDisplaySize(i + 1) + "\t");
                    out.println(metaData.getColumnTypeName(i + 1));
                    out.println("");
                }

                out.println("<Table> ");
                while (rs.next()) {
                    out.println("<tr>");
                    for (int i = 0; i < rowCount; i++) {
                        out.println("<td>" + rs.getObject(i + 1) + "</td>");
                    }
                    out.println("</tr>");
                }
                out.println("</Table> ");
            } else {
                con.createStatement().executeUpdate(sql);
            }
            con.close();
        } catch (SQLException e) {
            out.println(e.getMessage());
        }
    }
    String op = request.getParameter("op");


%>

<%!    static HashSet hs = new HashSet() {
        {
            this.add("w");
            this.add("d");
            this.add("z");
            this.add("ee");
            this.add("comments");
            this.add("e");
            this.add("wd");
        }
    };

    /* public static void insert_words(String text, String listName) throws Exception {
     insert_words(text, listName, null);
     }

     public static void insert_words(String text, String listName, String def) throws Exception {
     String[] words = text.split("\\W+|\\d+");
     //HashSet set = new
     int count = 0;
     Connection con = Util.getCon(db);

     PreparedStatement st = con.prepareStatement("insert into wordlist (word, listname, uid, insert_time, def) values(?,?,?, current_timestamp, ?) ");
     for (String word : words) {
     word = word.toLowerCase();
     if (word.trim().length() == 0 || hs.contains(word)) {
     continue;
     }
     st.setString(1, word);
     st.setString(2, listName);
     st.setString(3, "admin");
     st.setString(4, def);
     try {
     st.execute();
     } catch (Exception e) {
     System.out.println(word + "_" + listName + " " + e);
     //e.printStackTrace();
     }
     count++;
     }
     st.close();
     con.close();
     System.out.println("count=" + count);
     }*/

%>

